#!/bin/bash
[[ "${DEBUG}" == "true" ]] && set -x
 
function menu() {
    options=$(whiptail --title "$menu_title" \
    --menu "$menu_prompt" 24 60 17 \
    --cancel-button "Exit" --backtitle "Author: Elvis" --clear \
    "1" "$menu_content_1" \
    "2" "$menu_content_2" \
    "3" "$menu_content_3" \
    "4" "$menu_content_4" \
    "5" "$menu_content_5" \
    "6" "$menu_content_6" \
    "7" "$menu_content_7" \
    "8" "$menu_content_8" \
    "9" "$menu_content_9" \
    "10" "$menu_content_10" \
    "11" "$menu_content_11" \
    "12" "$menu_content_12" \
    "13" "$menu_content_13" \
    "14" "$menu_content_14" \
    "15" "$menu_content_15" \
    "16" "$menu_content_16" \
    "17" "$menu_content_17" 3>&1 1>&2 2>&3)
}

clear
#脚本位置
workHome=`pwd`

#
OLDIFS=$IFS
#脚本语言设置
export |grep -i SSH >/dev/null 2>&1
if [ "$?" != "0" ];then
   msg="Running this script under TTY Console is not supported. Please Use SSH client tools, such as putty,xshell\nPress Enter To Exit!"
   whiptail --backtitle "Author: Elvis" --title "ERROR" --msgbox "$msg" 10 70
   exit 0
fi

#语言选择
whiptail --backtitle "Author: Elvis" --title "Message" --yes-button "Chinese" --no-button "English" --yesno "Please select the display language of the script." 8 60
if [ "$?" == "0" ];then
   source $workHome/include/zh_cn.lang
else
   source $workHome/include/en_us.lang
fi

#判断系统版本
if [ -s /etc/redhat-release ]; then
     system_ver=`grep -oE  "[0-9.]+" /etc/redhat-release | cut -d . -f 1`
else
     system_ver=`grep -oE  "[0-9.]+" /etc/issue | cut -d . -f 1`
     echo -e "\e[31m错误！不支持的操作系统，退出\e[0m"
     exit 1
fi

#判断系统能否联网
echo -e "\e[32m$check_env_prompt\e[0m"
ping -c 3 -w 6 223.6.6.6 >/dev/null 2>&1
if [ "$?" != "0" ];then
   die_network="true"
else
   die_network="false"
fi

echo $0|grep -w "env.sh" >/dev/null 2>&1
if [ "$?" == "0" ];then
   whiptail -h>/dev/null 2>&1
   if [ "$?" != "0" ];then
      echo "安装whiptail" > /$workHome/logs/init.log
      if [ "$die_network" == "true" ];then
         echo -e "\033[31m$check_newt\033[0m"
         exit 1
      else
         echo -e "\033[33m$install_newt\033[0m"
         yum -y install newt qrencode>>/$workHome/logs/init.log 2>&1
      fi
   fi
fi
#二维码展示
echo "安装二维码生成软件,非必须，YUM安装有2分钟超时设置" >>/$workHome/logs/init.log
rpm -q qrencode >/dev/null 2>&1
if [ "$?" != "0" ];then
   rpm -ivh packages/extra/el${system_ver}/libpng*.rpm >>/$workHome/logs/init.log 2>&1
   rpm -ivh packages/extra/el${system_ver}/qrencode-libs*.rpm >>/$workHome/logs/init.log 2>&1
   rpm -ivh packages/extra/el${system_ver}/qrencode-[0-9]*.rpm >>/$workHome/logs/init.log 2>&1
fi

rpm -q qrencode >/dev/null 2>&1
if [ "$?" != "0" -a  "$die_network" = "false" ];then
   yum -y install epel-release >>/$workHome/logs/init.log 2>&1 &
   epelpid=$!
   sleep 60
   kill $epelpid   
   yum -y install qrencode >>/$workHome/logs/init.log 2>&1 &
   yumpid=$!
   sleep 60
   kill $yumpid
fi

#whiptail 窗口颜色设置  
if [ "$die_network" == "true" ];then
   export NEWT_COLORS='root=,color0 roottext=green,color0 shadow=,color0'
else
   export NEWT_COLORS='root=,color15 roottext=green,color7 shadow=,color15'
fi

clear

#主机IP获取
netdev=`ip route|grep "^default"|awk '{print $5}'`
ipaddr=`ip address show $netdev|grep "inet "|awk -F"/" '{print $1}'|awk '{print $2}'`

#日志记录文件
logs_file=/$workHome/logs/env.log.`date "+%Y-%m-%d_%H%M%S"`
echo "欢迎使用使用env.sh脚本" > $logs_file


while true
do
    menu
    if [ "$?" != "0" ];then
        sleep 1
        exit 0
    fi

    case $options in
    "1")
        source $workHome/include/sysOptimize.sh
        sys_optimize
        ;;
    "2")
        source $workHome/include/serviceOptimize.sh
        srv_optimize
        ;;
    "3")
        source $workHome/include/mysql.sh
        mysql_pre_set && \
        {
        mysql_env_check && 
          {
           if [ "$mysql_ins_type" == "1" ];then
              install_mysql_offline
           else
              install_mysql_online
           fi
          } && mysql_para_set 
        }|whiptail --backtitle "Author: Elvis" --title "$mysql_gauge" --gauge " " 6 60 0 && mysql_statu_check
        ;;
    "4")
        source $workHome/include/redis.sh
        install_redis
        ;;    
    "5")
        source $workHome/include/memcached.sh
        install_memcached
        ;;
    "6")
        source $workHome/include/nginx.sh
        install_nginx
        ;;
    "7")
        source $workHome/include/apache.sh
        install_apache
        ;;
    "8")
        source $workHome/include/php.sh
        install_php
        ;;
    "9")
        source $workHome/include/jdk.sh
        install_jdk
        ;;
    "10")
        source $workHome/include/jenkins.sh
        install_jenkins
        ;;
    "11")
        source $workHome/include/nexus.sh
        install_nexus
        ;;
    "12")
        source $workHome/include/csvn.sh
        install_csvn
        ;;
    "13")
        source $workHome/include/docker.sh
        install_docker
        ;;
    "14")
        source $workHome/include/localyum.sh
        yum_options
        ;;
    "15")
        source $workHome/include/sslcert.sh
        create_ssl_cert
        ;;
    "16")
        source $workHome/include/yumRepair.sh
        yum_repair
        ;;
    *)
        source $workHome/include/readme.sh
        readme
        ;;
    esac
    sleep 0.5
done
